<template>
	<div>
		<AplidForm ref="formRef" v-bind="BaseFormConfig" v-model="formModel" @change="handleItemChange" @submit="onSubmit"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { ElMessageBox } from 'element-plus'
import apiMod from '@/apiMod'
import { api, BaseFormConfig } from '../../config'
import { getAreaObj } from '@/utils/util'
import { useCommonStore } from '@/stores/common'
const { queryObj,setTempParams } = useCommonStore()

const emit = defineEmits(['refresh', 'back'])
const formModel: any = ref({})
const formRef = ref()
const onSubmit = (form, done) => {
	const crIds: any = getAreaObj(form.crAreas, 'cr')
	const crNames: any = getAreaObj(form.crAreasName, 'cr')

	const arIds: any = getAreaObj(form.areas, '')
	const arNames: any = getAreaObj(form.areasName, '')

	const unitAreaIds: any = getAreaObj(form.unitAreas, 'unit')
	const unitAreaNames: any = getAreaObj(form.unitAddress, 'unit')
	const params = {
		...form,
		...crIds,
		...crNames,
		...arIds,
		...arNames,
		...unitAreaIds,
		...unitAreaNames,
		unitAreas: undefined,
		unitAreasName: undefined,

		crAreas: undefined,
		crAreasName: undefined,
		areas: undefined,
		areasName: undefined,
		code: 2007,
    versionNumber:2,
    belongProject:queryObj.belongProject,
    subprojectId:queryObj.subprojectId,
	}
	setTempParams({
		idCard: params.idCard,
	})
	apiMod[api.add](params)
		.then((res) => {
			emit('refresh', res.data)
			done()
		})
		.catch((err) => {
			done()
		})
}

const handleItemChange = (model, item, e) => {
	// 校验身份证号是否已存在
	if (item.prop == 'idCard') {
		apiMod.view_dpFoundationPractitionersHome_info(e).then((res) => {
			const data = res.data
			if (data) {
        const cr_areas:any = getAreaObj(data, 'cr', 'set')
				const areas:any = getAreaObj(data, '', 'set')

         formModel.value = {
            ...data,
            crAreas: cr_areas.areaIds,
					crAreasName: cr_areas.names,

          areas: areas.areaIds,
					areasName: areas.names,
            id:undefined
         }
			}
		})
	}
}
const onSave = () => {
	formRef.value!.onSubmit()
}
defineExpose({
	onSave,
})
</script>

<style scoped></style>
